package com.zoweunion.mechanic.util.task;

import com.alibaba.fastjson.JSON;
import com.zoweunion.mechanic.config.RabbitMQForLoginConfig;
import com.zoweunion.mechanic.dao.PromotionCouponDao;
import com.zoweunion.mechanic.dao.UserCouponDao;
import com.zoweunion.mechanic.dao.app.ToolCarDataMapper;
import com.zoweunion.mechanic.dao.base.MessageRecordDao;
import com.zoweunion.mechanic.entity.PromotionCouponEntity;
import com.zoweunion.mechanic.entity.UserCouponEntity;
import com.zoweunion.mechanic.service.app.impl.ToolCarDataServiceImpl;
import com.zoweunion.mechanic.service.base.BaseService;
import com.zoweunion.mechanic.util.*;
import com.zoweunion.mechanic.util.constants.PushConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 车辆预警
 */
@Component
public class CarDataAbnormalTask {
    @Autowired
    private ToolCarDataMapper toolCarDataMapper;
    @Autowired
    private ToolCarDataServiceImpl toolCarDataServiceImpl;
    @Autowired
    private BaseService baseService;
    @Autowired
    private MessageRecordDao messageRecordDao;
    @Autowired
    private JiGuangPushUtil jiGuangPushUtil;
    @Autowired
    private PromotionCouponDao promotionCouponDao;
    @Autowired
    private UserCouponDao userCouponDao;
    @Autowired
    private RabbitMQForLoginConfig rmqlc;

    //车辆高温预警/1小时
    // 暂时注释掉，selectDataByOneHour里面的sql语句太耗性能
    // @Scheduled(cron = "0 0 0/1 * * ?")
    public void waterTemperatureTask() {
        //5,20分钟高温车辆
        List<String> thingsListFive = new ArrayList<>();
        List<String> thingsListTwenty = new ArrayList<>();
        //查询所有pc_car_data_real在线数据
        toolCarDataMapper.selectAllOnline().parallelStream().forEach(item -> {
            //获取表名
            String tableByThingsId = toolCarDataServiceImpl.selectTableByThingsId(item);
            List<Map<String, Object>> dataByOneHour =
                    toolCarDataMapper.selectDataByOneHour(tableByThingsId + "_tb1");
            List<Date> timeList = new ArrayList<>();
            dataByOneHour.forEach(dataHourList -> {
                if (Orgin.getIntValue(dataHourList, "WaterTemperature") > 100) {
                    Date sampleTime = Orgin.getDateTime(dataHourList, "sample_time");
                    timeList.add(sampleTime);
                }
                if (timeList.size() > 1 &&
                        (timeList.get(0).getTime() -
                                timeList.get(timeList.size() - 1).getTime() > 5 * 3600)) {
                    thingsListFive.add(item);
                }
                if (timeList.size() > 1 &&
                        (timeList.get(0).getTime() -
                                timeList.get(timeList.size() - 1).getTime() > 20 * 3600)) {
                    thingsListTwenty.add(item);
                }
            });
        });
//        if (thingsListFive.size() > 0) {
//            //5分钟推送
//            List<Map<String, Object>> userList = this.getUserList(thingsListFive, 8);
//            userList.forEach(item -> {
//                if (Orgin.getString(item, "advice_name") != null) {
//                    this.jPush(item, Orgin.getString(item, "o_id"), 1, 8);
//                    this.jPush(item, Orgin.getString(item, "c_id"), 1, 8);
//                    this.jPush(item, Orgin.getString(item, "kf_id"), 1, 8);
//                } else {
//                    this.jPush(item, Orgin.getString(item, "o_id"), 2, 8);
//                    this.jPush(item, Orgin.getString(item, "c_id"), 2, 8);
//                    this.jPush(item, Orgin.getString(item, "kf_id"), 2, 8);
//                }
//                //插入推送
//                messageRecordDao.addMessageRecordCount(Orgin.getString(item, "things_id"), 8);
//            });
//        }
//        if (thingsListTwenty.size() > 0) {
//            //20分钟推送
//            List<Map<String, Object>> userList = this.getUserList(thingsListTwenty, 9);
//            userList.forEach(item -> {
//                if (Orgin.getString(item, "advice_name") != null) {
//                    this.jPush(item, Orgin.getString(item, "o_id"), 1, 9);
//                    this.jPush(item, Orgin.getString(item, "c_id"), 1, 9);
//                    this.jPush(item, Orgin.getString(item, "kf_id"), 1, 9);
//                } else {
//                    this.jPush(item, Orgin.getString(item, "o_id"), 2, 9);
//                    this.jPush(item, Orgin.getString(item, "c_id"), 2, 9);
//                    this.jPush(item, Orgin.getString(item, "kf_id"), 2, 9);
//                }
//                //插入推送
//                messageRecordDao.addMessageRecordCount(Orgin.getString(item, "things_id"), 9);
//            });
//        }

    }

    public List<Map<String, Object>> getUserList(List<String> thingsList, Integer type) {
        String ids = "'" + StringUtils.join(thingsList, "','") + "'";
        return toolCarDataMapper.selectOwner(ids, type);
    }

    public void jPush(Map<String, Object> item, String userId, Integer type, Integer status, Integer count) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        //map.put("extra_params", JSON.toJSONString(item));
        if (type == 1) { //单参数
            String adviceName = Orgin.getString(item, "advice_name");
            Map<String, Object> messageRecord = null;
            String key = "";
            if (status == 8) {//高温
                content = String.format(SmsUtil.waringTemperatureFivee, adviceName, count);
                messageRecord = baseService.pushMessageTemperatureWarning(userId,
                        pushTime, content);
                map.put("send_type", 17);
                key = PushConstants.KEY_PUSH_CAR_WATER_TEMPERATURE_HIGH;
            }
//            else if (status == 9) {//持续高温
//                content = String.format(SmsUtil.waringtemperaturetwentye, adviceName);
//                messageRecord = baseService.pushMessageTemperatureHighWarning(userId,
//                        pushTime, content);
//                map.put("send_type", 18);
//                key = PushConstants.KEY_PUSH_CAR_WATER_TEMPERATURE_HIGH;
//            }
            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
           // messageRecord.put("push_user_id","bbc9aa19387b49f2a6103d402b7803ef");
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        } else if (type == 2) {//三参数
            String model = Orgin.getString(item, "model");
            String brand = Orgin.getString(item, "brand");
            String car_type = Orgin.getString(item, "car_type");
            Map<String, Object> messageRecord = null;
            String key = "";
            if (status == 8) {
                content = String.format(SmsUtil.waringTemperatureFive, model, brand, car_type, count);
                messageRecord = baseService.pushMessageTemperatureWarning(userId,
                        pushTime, content);
                map.put("send_type", 17);
                key = PushConstants.KEY_PUSH_CAR_WATER_TEMPERATURE_HIGH;
            }
//            else if (status == 9) {
//                content = String.format(SmsUtil.waringtemperaturetwenty, model, brand, car_type);
//                messageRecord = baseService.pushMessageTemperatureHighWarning(userId,
//                        pushTime, content);
//                map.put("send_type", 18);
//                key = PushConstants.KEY_PUSH_CAR_WATER_TEMPERATURE_HIGH;
//            }
            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        }
    }

    //液压油温度过低预警
    public void jPushHdlOilLowTemp(Map<String, Object> item, String userId, Integer type, Integer count) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        //map.put("extra_params", JSON.toJSONString(item));
        if (type == 1) { //单参数
            String adviceName = Orgin.getString(item, "advice_name");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.lowHdlOilWaringTemperatureTens, adviceName, count);
            messageRecord = baseService.pushMessageLowHdlOilTemperatureWarning(userId,
                    pushTime, content);
            map.put("send_type", 33);
            key = PushConstants.KEY_PUSH_CAR_HDLOIL_TEMPERATURE_LOW;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        } else if (type == 2) {//三参数
            String model = Orgin.getString(item, "model");
            String brand = Orgin.getString(item, "brand");
            String car_type = Orgin.getString(item, "car_type");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.lowHdlOilWaringTemperatureTen, model, brand, car_type, count);
            messageRecord = baseService.pushMessageLowHdlOilTemperatureWarning(userId,
                    pushTime, content);
            map.put("send_type", 33);
            key = PushConstants.KEY_PUSH_CAR_HDLOIL_TEMPERATURE_LOW;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        }
    }

    //机型对比推送
    public void jPushModelComparison( String userId, String user_name, String mobile_phone, Integer count, String model_name) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
       // map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());

            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.modelComparisonWarning, user_name, mobile_phone, count, model_name);
            messageRecord = baseService.pushMessageModelComparisonWarning(userId,
                    pushTime, content);
            map.put("send_type", 36);
            key = PushConstants.KEY_PUSH_USER_MODEL_COMPARISON;

            //map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
    }

    //新机二手机浏览推送
    public void jPushNewOrOldMachineBrowse(Map<String, Object> item, String userId, String user_name, String mobile_phone, String type_name) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());

        Map<String, Object> messageRecord = null;
        String key = "";

        content = String.format(SmsUtil.newOroldMachineBrowseWarning, user_name, mobile_phone, type_name);
        messageRecord = baseService.pushMessageNewOrOldMachineBrowseWarning(userId,
                pushTime, content);
        map.put("send_type", 37);
        key = PushConstants.KEY_PUSH_USER_BROSE_NEWOLDMACHINE;

        //map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
        jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
        //todo 推送记录 修改参数
        map.put("context", content);
        messageRecordDao.addMessageRecord(map);
    }

    //液压油温度过高预警
    public void jPushHdlOilHighTemp(Map<String, Object> item, String userId, Integer type, Integer count) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        //map.put("extra_params", JSON.toJSONString(item));
        if (type == 1) { //单参数
            String adviceName = Orgin.getString(item, "advice_name");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.highHdlOilWaringTemperatureFives, adviceName, count);
            messageRecord = baseService.pushMessageHighHdlOilTemperatureWarning(userId,
                    pushTime, content);
            map.put("send_type", 32);
            key = PushConstants.KEY_PUSH_CAR_HDLOIL_TEMPERATURE_HIGH;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
          //  messageRecord.put("push_user_id","bbc9aa19387b49f2a6103d402b7803ef");
           // messageRecord.put("push_user_id","975f00e6606f4c7ca5561e68007d1e65");
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        } else if (type == 2) {//三参数
            String model = Orgin.getString(item, "model");
            String brand = Orgin.getString(item, "brand");
            String car_type = Orgin.getString(item, "car_type");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.highHdlOilWaringTemperatureFive, model, brand, car_type, count);
            messageRecord = baseService.pushMessageHighHdlOilTemperatureWarning(userId,
                    pushTime, content);
            map.put("send_type", 32);
            key = PushConstants.KEY_PUSH_CAR_HDLOIL_TEMPERATURE_HIGH;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        }
    }

    //怠速时间过长报警
    public void jPushIdleTimeHigh(Map<String, Object> item, String userId, Integer type, Integer count) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        //map.put("extra_params", JSON.toJSONString(item));
        if (type == 1) { //单参数
            String adviceName = Orgin.getString(item, "advice_name");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.highIdleTimeThirtys, adviceName, count);
            messageRecord = baseService.pushMessageIdleTimeHighWarning(userId,
                    pushTime, content);
            map.put("send_type", 34);
            key = PushConstants.KEY_PUSH_CAR_IDLE_TIME_HIGH;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
          //  messageRecord.put("push_user_id","bbc9aa19387b49f2a6103d402b7803ef");
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        } else if (type == 2) {//三参数
            String model = Orgin.getString(item, "model");
            String brand = Orgin.getString(item, "brand");
            String car_type = Orgin.getString(item, "car_type");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.highIdleTimeThirty, model, brand, car_type, count);
            messageRecord = baseService.pushMessageIdleTimeHighWarning(userId,
                    pushTime, content);
            map.put("send_type", 34);
            key = PushConstants.KEY_PUSH_CAR_IDLE_TIME_HIGH;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        }
    }

    //无线设备被拆除报警
    public void jPushLightSenceWarning(Map<String, Object> item, String userId, Integer type) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String userName = Orgin.getString(item, "user_name");
        //map.put("extra_params", JSON.toJSONString(item));
        if (type == 1) { //单参数
            String adviceName = Orgin.getString(item, "advice_name");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.lightSenceWarnings, userName, adviceName);
            messageRecord = baseService.pushMessageLightSenceWarning(userId,
                    pushTime, content);
            map.put("send_type", 102);
            key = PushConstants.KEY_PUSH_CAR_LIGHT_SENCE_WARNING;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));

            // tdo 这里好像三种方法都使用到了
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            this.sendMQMessage(userId, content);
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        } else if (type == 2) {//三参数
            String model = Orgin.getString(item, "model");
            String brand = Orgin.getString(item, "brand");
            String car_type = Orgin.getString(item, "car_type");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.lightSenceWarning, userName, model, brand, car_type);
            messageRecord = baseService.pushMessageLightSenceWarning(userId,
                    pushTime, content);
            map.put("send_type", 102);
            key = PushConstants.KEY_PUSH_CAR_LIGHT_SENCE_WARNING;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            this.sendMQMessage(userId, content);
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        }

    }

    public void sendMQMessage(String userId, String content) {
        Map<String,Object> map = new HashMap<>();
        map.put("type", 3);
        map.put("content", content);
        map.put("title", PushConstants.TITLE_LIGHT_SENCE_WARNING);
        map.put("time", LocalDateTime.now().toString());
        String message = JSON.toJSONString(map);
        // String message = "{\"type\":3," +"\"content\":\"" + content + "\",\"title\":\"" + PushConstants.TITLE_LIGHT_SENCE_WARNING + "\"}";//type=3 pc端：光感报警弹窗推送
        // System.out.println("生产者产生的消息: " + message + userId);

        // routingKey 为虚拟的队列名称，与前端约定一致的uuid(设置为userId)(路由器需要设置为direct定向，把消息交给符合指定routing key的队列)
        RabbitTemplate rabbitTemplateForLightSenceWarning = rmqlc.rabbitTemplateForLogin();
        rabbitTemplateForLightSenceWarning.convertAndSend("amq.direct", userId, message);
    }

    // 机油压力过高报警
    public void jPushOilPressure(Map<String, Object> item, String userId, Integer type, Integer count) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        //map.put("extra_params", JSON.toJSONString(item));
        if (type == 1) { //单参数
            String adviceName = Orgin.getString(item, "advice_name");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.oilWaringPressureTwoMinus, adviceName, count);
            messageRecord = baseService.pushMessagePressureWarning(userId,
                    pushTime, content);
            map.put("send_type", 31);
            key = PushConstants.KEY_PUSH_CAR_OIL_PRESSURE_HIGH;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
//            messageRecord.put("push_user_id","bbc9aa19387b49f2a6103d402b7803ef");
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        } else if (type == 2) {//三参数
            String model = Orgin.getString(item, "model");
            String brand = Orgin.getString(item, "brand");
            String car_type = Orgin.getString(item, "car_type");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.oilWaringPressureTwoMinu, model, brand, car_type, count);
            messageRecord = baseService.pushMessagePressureWarning(userId,
                    pushTime, content);
            map.put("send_type", 31);
            key = PushConstants.KEY_PUSH_CAR_OIL_PRESSURE_HIGH;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        }
    }

    // 车辆保养提醒
    public void jPushServiceReminder(int engineTotalHours, Map<String, Object> item, String userId, Integer type) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        //map.put("extra_params", JSON.toJSONString(item));
        if (type == 1) { //单参数
            String adviceName = Orgin.getString(item, "advice_name");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.serviceReminderOneHundreds, adviceName, engineTotalHours);
            messageRecord = baseService.pushMessageServiceReminderWarning(userId,
                    pushTime, content);
            map.put("send_type", 35);
            key = PushConstants.KEY_PUSH_CAR_MAINTENANCE;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
           // messageRecord.put("push_user_id","bbc9aa19387b49f2a6103d402b7803ef");
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        } else if (type == 2) {//三参数
            String model = Orgin.getString(item, "model");
            String brand = Orgin.getString(item, "brand");
            String car_type = Orgin.getString(item, "car_type");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.serviceReminderOneHundred, model, brand, car_type, engineTotalHours);
            messageRecord = baseService.pushMessageServiceReminderWarning(userId,
                    pushTime, content);
            map.put("send_type", 35);
            key = PushConstants.KEY_PUSH_CAR_MAINTENANCE;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        }
    }

    // 机油温度过高预警
    public void jPushOilTemp(Map<String, Object> item, String userId, Integer type, Integer count) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        //map.put("extra_params", JSON.toJSONString(item));
        if (type == 1) { //单参数
            String adviceName = Orgin.getString(item, "advice_name");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.oilWaringTemperatureTwoMius, adviceName, count);
            messageRecord = baseService.pushMessageTemperatureWarning(userId,
                    pushTime, content);
            map.put("send_type", 30);
            key = PushConstants.KEY_PUSH_CAR_OIL_TEMPERATURE_HIGH;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
           // messageRecord.put("push_user_id","bbc9aa19387b49f2a6103d402b7803ef");
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            //todo 推送记录 修改参数
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        } else if (type == 2) {//三参数
            String model = Orgin.getString(item, "model");
            String brand = Orgin.getString(item, "brand");
            String car_type = Orgin.getString(item, "car_type");
            Map<String, Object> messageRecord = null;
            String key = "";

            content = String.format(SmsUtil.oilWaringTemperatureTwoMiu, model, brand, car_type, count);
            messageRecord = baseService.pushMessageTemperatureWarning(userId,
                    pushTime, content);
            map.put("send_type", 30);
            key = PushConstants.KEY_PUSH_CAR_OIL_TEMPERATURE_HIGH;

            map.put("extra_params", this.getExtraParams(key, JSON.toJSONString(item)));
            jiGuangPushUtil.publishMessagePush(messageRecord, key, "", map.get("id").toString());
            map.put("context", content);
            messageRecordDao.addMessageRecord(map);
        }
    }

    //金融平台：用户还款提醒(7天、3天、1天)
    public void jPushRemindRepaymentByDay(Map<String, Object> item, String userId, Integer day) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String adviceName = Orgin.getString(item, "advice_name") != "" ? Orgin.getString(item, "advice_name") : "无";
        String model = Orgin.getString(item, "model") != "" ? Orgin.getString(item, "model") : "无";
        String brand = Orgin.getString(item, "brand")!= "" ? Orgin.getString(item, "brand") : "无";
        String car_type = Orgin.getString(item, "car_type")!= "" ? Orgin.getString(item, "car_type") : "无";
        content = String.format(SmsUtil.WarningRemindRepaymentByDay, adviceName, model, brand, car_type, day);
        Map<String, Object> messageRecord = baseService.pushMessageRemindRepaymentByDayParams(userId,
                pushTime, content, map);
        jiGuangPushUtil.publishMessagePush(messageRecord, PushConstants.KEY_JUMP_URL,"/carRemindRepayment", map.get("id").toString());
        //todo 推送记录 修改参数
        map.put("context", content);
        map.put("send_type", 107); // 还款提醒 t_s_ss_message_record 的 send_type 为 107
        map.put("extra_params", this.getExtraParams(PushConstants.KEY_JUMP_URL, JSON.toJSONString(item)));
        messageRecordDao.addMessageRecord(map);
    }

    // 优惠券预警
//    @Scheduled(cron = "0 0 9 * * ?")
    public void couponPush() {
        List<PromotionCouponEntity> entities = promotionCouponDao.couponListByDate();
        entities.forEach(item -> {
            if (item.getValidityDate() < item.getExpireNotice()) {
                String mobilePhone = item.getMobilePhone();
                String id = item.getUserId();
                SmsUtil.sendSms(mobilePhone, item.getNoteTemplate());
                if (item.getValidityDate() == 3) {
                    this.sendPush(item, id);
                }
                if (item.getValidityDate() == 7) {
                    this.sendPush(item, id);
                }
                if (item.getValidityDate() == 15) {
                    this.sendPush(item, id);
                }
            }
        });
    }

    public void sendPush(PromotionCouponEntity item, String id) {
        //推送通知
        Map<String, Object> map1 = new HashMap<>();
        map1.put("s_id", "");
        map1.put("receive_user", id);
        map1.put("client", "YD");
        map1.put("id", UuidUtil.get32UUID());
        String content = String.format(SmsUtil.couponinv, item.getCouponMoney()
                , item.getValidityDate());
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        Map<String, Object> messageRecord = baseService.pushMessageCoupon(id,
                pushTime, content);
        jiGuangPushUtil.publishMessagePush(messageRecord, "", "", id);
    }

    //优惠券过期
//    @Scheduled(cron = "0 0 1 * * ?")
    public void couponExceed() {
        List<PromotionCouponEntity> entities = promotionCouponDao.selectCouponListByDate();
        entities.forEach(item -> {
            if (item.getValidityDate() < 0) {
                userCouponDao.updateById(UserCouponEntity.builder()
                        .id(item.getId())
                        .deleteFlag(1)
                        .build());
                promotionCouponDao.updateById(PromotionCouponEntity.builder()
                        .id(item.getCouponId())
                        .deleteFlag(1)
                        .build());
            }
        });
    }

    //车辆到期提醒推送
    //@Scheduled(cron = "0 0 9 * * ?")
    public void carAnnualTimeNotice() {
        List<Map<String, Object>> entities = promotionCouponDao.carAnnualTimeNotice();
        entities.parallelStream().forEach(item -> {
            String id = Orgin.getString(item, "id");
            String adviceName = Orgin.getString(item, "advice_name");
            String content = String.format(SmsUtil.caannual, adviceName);
            Map<String, Object> messageRecord = baseService.carAnnual(id,
                    DateUtil.formatTime(LocalDateTime.now()), content);
            jiGuangPushUtil.publishMessagePush(messageRecord, "", "", id);
        });
    }


    public void jPushFuel(Map<String, Object> item, String userId, Integer fuelGauge) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String adviceName = Orgin.getString(item, "advice_name");
        content = String.format(PushConstants.TEMPLATE_CAR_FUEL_GUAGE_LOW_NEW, adviceName, fuelGauge);
        Map<String, Object> messageRecord = baseService.pushMessageCarFuelGaugeParams(userId,
                pushTime, content, map);
        jiGuangPushUtil.publishMessagePush(messageRecord, PushConstants.KEY_PUSH_CAR_FUEL_GUAGE_LOW, "", map.get("id").toString());
        //todo 推送记录 修改参数
        map.put("context", content);
        map.put("send_type", 15);
        map.put("extra_params", this.getExtraParams(PushConstants.KEY_PUSH_CAR_FUEL_GUAGE_LOW, JSON.toJSONString(item)));
        messageRecordDao.addMessageRecord(map);
    }

    public void jPushFmiDescription(Map<String, Object> item, String userId, String fmiDescription, Integer count) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String adviceName = Orgin.getString(item, "advice_name");
        content = String.format(PushConstants.TEMPLATE_CAR_FAULT_NEW, adviceName, count, fmiDescription);
        Map<String, Object> messageRecord = baseService.pushMessageCarFuelGaugeParams(userId, pushTime, content, map);
        String value = JSON.toJSONString(item);
      //  messageRecord.put("push_user_id","bbc9aa19387b49f2a6103d402b7803ef");
        jiGuangPushUtil.publishMessagePush(messageRecord, PushConstants.KEY_PUSH_CAR_FAULT, value, map.get("id").toString());
        map.put("context", content);
        map.put("send_type", 16);
        map.put("extra_params", this.getExtraParams(PushConstants.KEY_PUSH_CAR_FAULT, value));
        messageRecordDao.addMessageRecord(map);
    }

    private String getExtraParams(String key, String value) {
        Map<String, Object> extraParamsMap = new HashMap<>();
        extraParamsMap.put(key, value);
        return JSON.toJSONString(extraParamsMap);
    }

    /**
     * 还款期前还款成功 - 手机APP极光推送
     * @param item
     */
    public void jPushAccountingPeriod(Map<String, Object> item) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", item.get("o_id").toString());
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String adviceName = Orgin.getString(item, "advice_name");
        String thingsId = Orgin.getString(item, "things_id");
        content = String.format(PushConstants.TEMPLATE_ACCOUNTING_PERIOD, adviceName, thingsId);
        Map<String, Object> messageRecord = baseService.pushMessageAccountingPeriod(item.get("o_id").toString(),
                pushTime, content, map);
        String value = JSON.toJSONString(item);
        jiGuangPushUtil.publishMessagePush(messageRecord, PushConstants.KEY_PUSH_CAR_ACCOUNTING_PERIOD, value, map.get("id").toString());
        map.put("context", content);
        map.put("send_type", 200); // 还款期前还款成功的消息类型为 200 (从200起始)
        map.put("extra_params", this.getExtraParams(PushConstants.KEY_PUSH_CAR_ACCOUNTING_PERIOD, value));
        messageRecordDao.addMessageRecord(map);
    }

    /**
     * 补缴欠款成功 - 手机APP极光推送
     * @param item
     */
    public void jPushSupplementaryPayment(Map<String, Object> item) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", item.get("o_id").toString());
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String adviceName = Orgin.getString(item, "advice_name");
        String thingsId = Orgin.getString(item, "things_id");
        content = String.format(PushConstants.TEMPLATE_SUPPLEMENTARY_PAYMENT, adviceName, thingsId);
        Map<String, Object> messageRecord = baseService.pushMessageSupplementaryPayment(item.get("o_id").toString(),
                pushTime, content, map);
        String value = JSON.toJSONString(item);
        jiGuangPushUtil.publishMessagePush(messageRecord, PushConstants.KEY_PUSH_CAR_SUPPLEMENTARY_PAYMENT, value, map.get("id").toString());
        map.put("context", content);
        map.put("send_type", 201); // 补缴欠款成功的消息类型为 201 (从200起始)
        map.put("extra_params", this.getExtraParams(PushConstants.KEY_PUSH_CAR_SUPPLEMENTARY_PAYMENT, value));
        messageRecordDao.addMessageRecord(map);
    }

    /**
     * 解锁申请已通过 - 手机APP极光推送
     * @param item
     */
    public void jPushApplicationPassed(Map<String, Object> item) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", item.get("o_id").toString());
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String adviceName = Orgin.getString(item, "advice_name");
        String thingsId = Orgin.getString(item, "things_id");
        content = String.format(PushConstants.TEMPLATE_APPLICATIONPASSED, adviceName, thingsId);
        Map<String, Object> messageRecord = baseService.pushMessageApplicationPassed(item.get("o_id").toString(),
                pushTime, content, map);
        String value = JSON.toJSONString(item);
        jiGuangPushUtil.publishMessagePush(messageRecord, PushConstants.KEY_PUSH_CAR_APPLICATIONPASSED, value, map.get("id").toString());
        map.put("context", content);
        map.put("send_type", 202); // 解锁申请已通过的消息类型为 202 (从200起始)
        map.put("extra_params", this.getExtraParams(PushConstants.KEY_PUSH_CAR_APPLICATIONPASSED, value));
        messageRecordDao.addMessageRecord(map);
    }

    /**
     * 解锁申请已拒绝 - 手机APP极光推送
     * @param item
     */
    public void jPushApplicationReject(Map<String, Object> item) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", item.get("o_id").toString());
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String adviceName = Orgin.getString(item, "advice_name");
        String thingsId = Orgin.getString(item, "things_id");
        content = String.format(PushConstants.TEMPLATE_APPLICATIONREJECT, adviceName, thingsId);
        Map<String, Object> messageRecord = baseService.pushMessageApplicationReject(item.get("o_id").toString(),
                pushTime, content, map);
        String value = JSON.toJSONString(item);
        jiGuangPushUtil.publishMessagePush(messageRecord, PushConstants.KEY_PUSH_CAR_APPLICATIONREJECT, value, map.get("id").toString());
        map.put("context", content);
        map.put("send_type", 203); // 解锁申请已拒绝的消息类型为 203 (从200起始)
        map.put("extra_params", this.getExtraParams(PushConstants.KEY_PUSH_CAR_APPLICATIONREJECT, value));
        messageRecordDao.addMessageRecord(map);
    }

    public void jPushFaileDescription(Map<String, Object> item, String userId) {
        String pushTime = DateUtil.formatTime(LocalDateTime.now());
        String content = "";
        Map<String, Object> map = new HashMap<>();
        map.put("s_id", item.get("s_id").toString());
        map.put("receive_user", userId);
        map.put("client", "YD");
        map.put("id", UuidUtil.get32UUID());
        String adviceName = Orgin.getString(item, "advice_name");
        content = String.format(PushConstants.TEMPLATE_CAR_FAULT_ELECTRIC, adviceName);
        Map<String, Object> messageRecord = baseService.pushMessageElectricCarFailParams(userId,
                pushTime, content, map);
        String value = JSON.toJSONString(item);
        jiGuangPushUtil.publishMessagePush(messageRecord, PushConstants.KEY_PUSH_ELECTRIC_CAR_FAULT, value, map.get("id").toString());
        map.put("context", content);
        map.put("send_type", 204); // 电车的故障类型为 204 (从200起始)
        map.put("extra_params", this.getExtraParams(PushConstants.KEY_PUSH_ELECTRIC_CAR_FAULT, value));
        messageRecordDao.addMessageRecord(map);
    }
}